有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Javabat帮助:alarmClock

我正在处理JavaBat问题,对自己的逻辑感到困惑

任务如下:

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".

alarmClock(1, false) → "7:00" alarmClock(5, false) → "7:00" alarmClock(0, false) → "10:00"

这是我的密码:

            public String alarmClock(int day, boolean vacation) {
                if ( (day >=1 && day <=5) && (!vacation)) {
                    return "7:00";
                } else if ( (day >=1 && day <=5) && (vacation)) {
                    return "10:00";  
                } else {
                return "off";
                }  
            }

为什么这两项测试都失败了

报警时钟(0,假)→ “10:00”“关闭”X
报警时钟(6,假)→ “10:00”“关闭”X

当然,这条线涵盖了它

如果(天>;=1&;&;天<;=5)&&;(!假期))


共 (5) 个答案

  1. # 1 楼答案

    public String alarmClock(int day, boolean vacation) {
    
      if (((day==0)||(day==6))&&(!vacation)){
      return "10:00";
      }
      else if (((day!=0)||(day!=6))&&(!vacation)){
      return "7:00";
      }
      else if (((day==0)||(day==6))&&(vacation)){
      return "off";
      }
      else{
      return "10:00";
      }
    
    
    
    
    }
    
  2. # 2 楼答案

    Surely, this line covers it?

    if ((day >=1 && day <=5) && (!vacation)) 
    

    不,那条线不包括它。如果日期是星期日或星期六(0或6),则“and”表达式的第一部分(day >=1 && day <=5)将为false,因为0和6不在1和5之间(包括1和5)

    处理天06的唯一分支是您的else分支:“off”

    这是一个很好的时机,可以使用helper方法来表达更接近英文描述的逻辑:

    if ( isWeekday(day) ) {
        if ( vacation ) {
           //what to return here?
        } else {
           //what to return here?
        }
    } else {
        if ( vacation ) {
           //what to return here?
        } else {
           //what to return here?
        }
    }
    

    然后您只需要实现isWeekday

    private boolean isWeekday(int day) {
       return /*fill this in*/;
    }
    
  3. # 3 楼答案

    if ((day == 0 || day == 6) && (!vacation) || (day >= 1 && day <= 5) && (vacation)) {
            return "10:00";
        }
        if ((day >= 1 && day <= 5) && (!vacation)) {
            return "7:00";
        }
    
        return "off";
    
  4. # 4 楼答案

    虽然三元运算符在这里很有用,因为这个问题可以在一个返回语句中完成,但下面的代码允许可读性

    public String alarmClock(int day, boolean vacation) {
          if(vacation){ //if we are on vacation
             if(day > 0 && day < 6){ //if it is weekday and we are on vacation
             return "10:00";
             }
             else return "off"; //it must be the weekend!
          } 
          //from here on out all the cases where vacation is true have been weeded out
          if(day > 0 && day < 6){
          return "7:00";
          }
          else return "10:00";
    }
    
  5. # 5 楼答案

    这个怎么样

          public String alarmClock(int day, boolean vacation) {
                if (day >=1 && day <=5) {
                    return vacation ? "10:00" : "7:00";
                } else {
                    return vacation ? "off" : "10:00";
                }  
            }
    

    注意,这取决于您的编码约定是否允许使用turnary运算符。但在这种情况下,我认为逻辑更容易理解